#!/bin/bash
#############################
##安装php扩展
#############################

starttime=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$starttime" +%s);

#shell颜色变量
RED_COLOR='\E[1;31m'
GREEN_COLOR="\E[1;32m"
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
PINK_COLOR='\E[1;35m'
RES='\E[0m'

#引入公共自定义函数方法
wget https://gitee.com/guer168/dockerfiles/raw/master/func.sh
chmod +x func.sh
source ./func.sh


#获取系统版本号判断是否支持
system_version=$(cat /etc/redhat-release | grep release | awk '{print $4}')
OLD_IFS="$IFS"
IFS="."
version_array=($system_version)
IFS="$OLD_IFS"
if [ ${version_array[0]} != "7" ] ; then
	echo "必须centos7系统才能使用本脚本！"; exit
fi

#获取所有已安装版本
php_path=()
full_v_num=()
short_v_num=()
i=0
select_str=""
installed_phps=`find / -name php`
arr_path=($installed_phps)
for check_php_v in ${arr_path[@]}
do
    str_v=`$check_php_v -v`
	copyright="Copyright"
	result=$(echo $str_v | grep "${copyright}")
	if [ "$result" != "" ]; then
		arr_v=($str_v)
		php_path[i]=${check_php_v}
		full_v_num[i]=${arr_v[1]}
		sentence="${arr_v[1]}"
		OLD_IFS="$IFS"
		IFS="."
		arr=($sentence)
		IFS="$OLD_IFS"
		short_v_num[i]="${arr[0]}.${arr[1]}"
		if [ $i -gt 0 ]; then
			select_str="${select_str} ${i}.php${short_v_num[i]} "
		fi
		i=`expr $i + 1`;
	fi
done

#安装选择
echo -e "${GREEN_COLOR} ${select_str} ${RES}"
read -p " 选择PHP版本：" install_php_version

#创建软件下载目录  
mkdir -p  /home/soft && cd /home/soft

#更新并安装编译依赖相关软件
yum -y update
systemctl disable systemd-logind
systemctl mask systemd-logind
yum -y install make gcc gcc-c++ automake autoconf git vim wget
yum -y install libstdc++.so.6 curl curl-devel openssl-devel kernel-devel net-tools psmisc
yum -y install php-devel php-pear

#下载进入ext目录
if [ ! -f "php-${full_v_num[install_php_version]}.tar.gz" ]; then
	wget --no-check-certificate https://www.php.net/distributions/php-${full_v_num[install_php_version]}.tar.gz
fi
tar -zxvf php-${full_v_num[install_php_version]}.tar.gz
cd php-${full_v_num[install_php_version]}/ext

#构建选择提示文字
f=1
modules_str=`ls -l |awk '/^d/ {print $NF}'`
all_modules=($modules_str)
ext_modules=("swoole" "redis" "mysql")
arr_modules=("${all_modules[@]}" "${ext_modules[@]}")
arr_modules=($(array_unique "${arr_modules[*]}"))
#移除相应元素
#delete=(zlib)
#arr_modules=(${all_modules[@]/$delete})
modules_newstr="0.全部\n"
for m in ${arr_modules[@]}
do
	modules_newstr="${modules_newstr} ${f}.${m}\n"
    f=`expr $f + 1`;
done

#选择要安装模块
echo -e "${GREEN_COLOR} ${modules_newstr} ${RES}"
read -p " 选择PHP安装模块(多个模块逗号隔开 例如:1,2,5,7):" install_php_modules
#转换输入，=>，
install_php_modules="${install_php_modules/，/,}"
#输入结果转换成数组
OLD_IFS="$IFS"
IFS="," 
arr_install_php_modules=($install_php_modules)
IFS="$OLD_IFS"



#依次处理结果
for s in ${arr_install_php_modules[@]}
do
	module_name="${arr_modules[s-1]}"
	cd "$(pwd)/${module_name}"
	#配置文件路径
	ini_path_str=`/usr/local/php/${short_v_num[install_php_version]}/bin/php -i | grep "Loaded Configuration File"`
	arr_ini_path=(ini_path_str)
	ini_path="${arr_ini_path[4]}"
	
	#判断是否已经安装
	check_m=`/usr/local/php/${short_v_num[install_php_version]}/bin/php -m | grep ${module_name}`
	if [ "$check_m" = "$module_name" ]; then
		typeset -l act_module
		echo -e "${RED_COLOR} $(pwd)已安装${module_name}模块 ${RES}";
		read -p " 选择操作方法(reinstall/pass/uninstall):" act_module
		#转换输入r=>reinstall,p=>pass,u=>uninstall
		act_module="${act_module/r/reinstall}"
		act_module="${act_module/p/pass}"
		act_module="${act_module/u/uninstall}"
		
		#跳过不处理
		if [ "$act_module" = "pass" ]; then
			cd ../
			continue
		fi
		#卸载处理
		if [ "$act_module" = "uninstall" ] ; then
			str_replace_file "^extension = \"${module_name}.so\"" ";extension = \"${module_name}.so\"" "$ini_path"
		fi
	fi
	
	domake="y"
	#[[模块特殊处理
	if [ "$module_name" = "zlib" ]; then
		if [ -f "config0.m4" ]; then
			cp -f config0.m4 config.m4
		fi
	fi
	if [ "$module_name" = "swoole" ]; then
		pecl install swoole
		domake="n"
	fi
	#模块特殊处理]]
	
	#编译安装
	if [ "$domake" = "y" ]; then
		/usr/local/php/${short_v_num[install_php_version]}/bin/phpize
		phpzie_res=`echo $?`
		if [ "$phpzie_res" != "0" ] ; then
			echo "$(pwd)运行phpzie出错"; exit
		fi
		./configure --with-php-config=/usr/local/php/${short_v_num[install_php_version]}/bin/php-config
		configure_res=`echo $?`
		if [ "$configure_res" != "0" ] ; then
			echo "$(pwd)运行配置出错"; exit
		fi
		make && make install
		install_res=`echo $?`
		if [ "$install_res" != "0" ] ; then
			echo "$(pwd)编译安装出错"; exit
		fi
	fi
	#配置php.ini
	str_after_add_file "^; extension_dir = \"ext\"" "extension = \"${module_name}.so\"" "$ini_path"
	
	cd ../
done


#删除所有解压文件夹
#ls -l |awk '/^d/ {print $NF}' | xargs rm -rf && rm -rf *.rpm.*
#统计运行耗时
rm -rf ./func.sh
endtime=`date +'%Y-%m-%d %H:%M:%S'`
end_seconds=$(date --date="$endtime" +%s);
echo "$starttime ---> $endtime" "全部安装完成,总共耗时:"$((end_seconds-start_seconds))"秒"